package com.jt.msg.resp;

import com.jt.msg.DataPacket;
import com.jt.utils.JT808Const;
import io.netty.buffer.ByteBuf;

/**
 * 通用应答包
 *
 * @author wang xiao
 * date 2023/8/9
 */

public class CommonResp extends DataPacket {

    /**
     * 成功/确认
     */
    public static final byte SUCCESS = 0;

    /**
     * 失败
     */
    public static final byte FAILURE = 1;

    /**
     * 消息有误
     */
    public static final byte MSG_ERROR = 2;

    /**
     * 不支持
     */
    public static final byte UNSUPPORTED = 3;

    /**
     * 报警处理确认
     */
    public static final byte ALARM_PROCESS_ACK = 4;

    /**
     * 应答流水号 2字节
     */
    private short replyFlowId;

    /**
     * 应答 ID  2字节
     */
    private short replyId;

    /**
     * 结果 1字节
     */
    private byte result;

    public CommonResp() {
        this.getHeader().setMsgId(JT808Const.SERVER_RESP_COMMON);
    }

    public static CommonResp success(DataPacket msg, short flowId) {
        CommonResp resp = new CommonResp();
        resp.getHeader().setTerminalId(msg.getHeader().getTerminalId());
        resp.getHeader().setFlowId(flowId);
        resp.setReplyFlowId(msg.getHeader().getFlowId());
        resp.setReplyId(msg.getHeader().getMsgId());
        resp.setResult(SUCCESS);
        return resp;
    }

    @Override
    public ByteBuf toByteBufMsg() {
        ByteBuf bb = super.toByteBufMsg();
        bb.writeShort(replyFlowId);
        bb.writeShort(replyId);
        bb.writeByte(result);
        return bb;
    }

    public short getReplyFlowId() {
        return replyFlowId;
    }

    public void setReplyFlowId(short replyFlowId) {
        this.replyFlowId = replyFlowId;
    }

    public short getReplyId() {
        return replyId;
    }

    public void setReplyId(short replyId) {
        this.replyId = replyId;
    }

    public byte getResult() {
        return result;
    }

    public void setResult(byte result) {
        this.result = result;
    }
}
